<?xml version="1.0" encoding="UTF-8"?>
<%@page contentType="text/html; charset=UTF-8"
		import="java.util.Calendar,
		        java.math.BigDecimal" %>
<%
	Session hSession = HibernateUtil.getSessionFactory().getCurrentSession();

	String action = request.getParameter("action");
	String id = request.getParameter("id");

	if ("ADD".equals(action)) {
		Category c = new Category();
		c.setId(Long.valueOf(0));
		
		Author a = new Author();
		a.setId(Long.valueOf(0));
		
		Book b = new Book();
		b.setTitle("");
		b.setDate(Calendar.getInstance().getTime());
		b.setCategory(c);
		b.addAuthor(a);
		b.setPrice(BigDecimal.ZERO);
		
		SessionUtil.setBook(session, b);
		
	} else if ("EDIT".equals(action)) {
		Book b = (Book) hSession.get(Book.class, Long.valueOf(id));
		
		SessionUtil.setBook(session, b);
	}

	Book book = SessionUtil.getBook(session);
	Boolean creation = (book.getId() == null);
	
	Collection<Category> categories = hSession.createCriteria(Category.class).list();
	Collection<Author> authors = hSession.createCriteria(Author.class).list();
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr">
<head>
	<% if (creation) { %>
	<title>Bookstore :: Back office :: Ajouter un livre</title>
	<% } else { %>
	<title>Bookstore :: Back office :: Modifier un livre</title>
	<% } %>
	<%@include file="include.jsp" %>
</head>

<body>
	<%@include file="commun.jsp" %>
	
	<div class="contenuAvecMenu">
		<% if (creation) { %>
		<h2>Ajouter un livre</h2>
		<% } else { %>
		<h2>Modifier un livre</h2>
		<% } %>
		<form method="post" action="SaveBook.do">
			<div class="boite bleue">
				<p>
					<label for="title">Titre</label>
					<input type="text" id="title" name="title" value="<%=book%>" />
				</p>
				
				<p>
					<label for="price">Prix</label>
					<input type="text" id="price" name="price" value="<%=book.getPrice()%>" />
					&euro;
				</p>
				
				<p>
					<label for="date">Date</label>
					<input type="text" id="date" name="date" value="<%=FormatUtil.formatDate(book.getDate())%>" />
					(JJ/MM/AA)
				</p>
				
				<p>
				<label for="category">Categorie</label>
				<select id="category" name="category">
					<option value="0"></option>
				<% for (Category category : categories) { %>
					<% if (category.equals(book.getCategory())) { %>
					<option selected="selected" value="<%=category.getId()%>"><%=category%></option>
					<% } else { %>
					<option value="<%=category.getId()%>"><%=category%></option>
					<% } %>
				<% } %>
				</select>
				</p>
				
				<select id="authorModel" style="display:none;">
					<option value="0"></option>
				<% for (Author author : authors) { %>
					<option value="<%=author.getId()%>"><%=author%></option>
				<% } %>
				</select>
				
				<div id="authors">
				<%
					int i = 0;
					for (Author bookAuthor : book.getAuthors()) {
				%>
					<p>
					<label id="label<%=i%>">Auteur <%=i+1%></label>
					
					<select id="author<%=i%>" name="author<%=i%>">
						<option value="0"></option>
					<% for (Author author : authors) { %>
						<% if (author.equals(bookAuthor)) { %>
						<option selected="selected" value="<%=author.getId()%>"><%=author%></option>
						<% } else { %>
						<option value="<%=author.getId()%>"><%=author%></option>
						<% } %>
					<% } %>
					</select><a id="remove<%=i%>" href="javascript:removeAuthor(<%=i%>);">X</a>
					</p>
				<%
						i++;
					}
				%>
				</div>
				<img src="../resources/images/add.png" alt="" />
				<a href="javascript:addAuthor()">Ajouter un auteur</a>
				<br />
				
				<input type="submit" value="Sauvegarder" />
			</div>
		</form>
	</div>
</body>
</html>
